package com.liang.book.controller;

import com.liang.book.enums.StatusEnums;
import com.liang.book.model.*;
import com.liang.book.service.BookService;
import com.liang.book.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {

    @Autowired
    private  BookService bookService;
    @Autowired
    private UserService userService;

    @RequestMapping("/getListPage")
    public Result<PageResult<BookInfo>> getListByPage(RequestPage requestPage) {
     log.info("getListByPage 接受参数：{}",requestPage);

            PageResult<BookInfo> result = bookService.getListByPage( requestPage);
//            String userName = (String)session.getAttribute("userName");
//            String description = userService.findRole(userName).getDescription();
            return Result.success(result);
        }


    @RequestMapping("/addBook")
    public Boolean addBook(BookInfo bookInfo) {
      log.info(" BookController.addBook 接收参数：{}",bookInfo);
        //1.参数校验
        if(!StringUtils.hasLength(bookInfo.getBookName()) ||
        !StringUtils.hasLength(bookInfo.getAuthor()) ||
          bookInfo.getCount() == null ||
          bookInfo.getPrice() == null ||
        !StringUtils.hasLength(bookInfo.getPublish())
          ) {
         log.warn("参数不正确");
            return false;
        }
        Integer result = bookService.addBook(bookInfo);
        if(result > 0) {
//            log.info("添加图书成功");
            return true;
        }
        log.error("添加图书失败");
        return false;
        //2.操作数据库
        //3.返回结果
    }

    @RequestMapping("/getBookInfo")
    public BookInfo getBookInfo(Integer bookId) {
        log.info("getBookInfo , bookId:{}",bookId);
        BookInfo bookInfo = bookService.getBookInfo(bookId);
        if(bookInfo  == null) {
            return new BookInfo();
        }
        return bookInfo;
    }

    @RequestMapping("/updateBook")
    public Boolean updateBook(BookInfo bookInfo) {
        log.info("updateBook ，bookInfo: {}",bookInfo);
       if(bookInfo.getId() == null ){
           log.warn("Id不能为空");
           return false;
       }
       if(bookInfo.getBookName() != null && "".equals(bookInfo.getBookName().trim())) {
           log.warn("图书名称不能为空");
           return false;
       }
       try {
           Integer result = bookService.updateBook(bookInfo);
           if(result > 0) {
               return true;
           }
           return false;
       }catch (Exception e) {
           log.error("updateBook error,e :{}",e);
           return false;
       }

    }
  @RequestMapping("/deleteBook")
    public Boolean deleteBook(Integer id) {
        log.info("deleteBook,id:{}" + id);
        try{
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(id);
            bookInfo.setStatus(StatusEnums.DELETED.getCode());
            Integer result = bookService.updateBook(bookInfo);
            if(result > 0) {
                return true;
            }
            return false;
        }catch (Exception e) {
            log.error("deleteBook error,e : {}",e);
            return false;
        }
  }

  @RequestMapping("/batchDelete")
    public Boolean batchDelete(@RequestParam List<Integer> ids) { //List的话，记得加@RequestParam   //如果前端传的是json 记得加@RequestBody
        log.info("batchDelete,ids:{}" ,ids);
        if(ids == null || ids.size() == 0) {
            return true;
        }
        try{
            Integer result = bookService.batchDelete(ids);
            if(result > 0) {
                return true;
            }
            return false;
        }catch (Exception e) {
            log.error("batchDelete error:", e);
            return false;
        }

  }

//    @GetMapping("/getBorrowedBooks")
//    public List<BorrowedRecords> getBorrowedBook(HttpSession session) {
//        String userName = session.getAttribute("userName").toString();
//        System.out.println(userName);
//        List<BorrowedRecords> records = bookService.getBorrowingRecords(userName);
//        System.out.println(records);
//        return records;
//    }

    @GetMapping("/getBorrowedBooks")
    public List<BorrowedRecords> getBorrowedBook() {
        List<BorrowedRecords> records = bookService.getBorrowingRecords();
        System.out.println(records);
        return records;
    }



    @RequestMapping(value = "/updateBorrowingRecords")
    public Boolean updateBorrowingRecords(BorrowedRecords record) {
        log.info("updateBorrowingRecordsrecord: {}",record);
        System.out.println("接收到record数据："+record);
        System.out.println("updateBorrowingRecords");
//        if(record.getBookName() == null ){
//            log.warn("借阅图书不能为空");
//            return false;
//        }
        try {
            Integer result = bookService.updateBorrowingRecords(record);
            System.out.println("result"+ result);
            if(result > 0) {
                return true;
            }
        }catch (Exception e) {
            log.error("updateBook error,e :{}",e);
            return false;
        }
        return false;
    }

    @RequestMapping(value = "/findBorrowingRecords",produces = "application/json")
    public List<BorrowedRecords> findBorrowingRecords(String userName) {
        System.out.println("kkk:"+bookService.findBorrowingRecords(userName));
        return  bookService.findBorrowingRecords(userName);
    }

}
